 #!/usr/bin/python

 #🍺本文件主要实现请求对应的查询接口，获取用户状态
 ##目前支持：
 ##- antpool
 ##- f2pool
 ##- poolin

import time
import hmac,hashlib
import ssl,requests
import sys

#global data
coin_type=''
sign_id=''
sign_key=''#api
sign_SECRET=''#sec
html_poolstats='https://antpool.com/api/poolStats.htm'
html_balance='https://antpool.com/api/account.htm'
html_hashrate_user='https://antpool.com/api/hashrate.htm'
html_hashrate_miner='https://antpool.com/api/workers.htm'
html_payment='https://antpool.com/api/paymentHistory.htm'

def getinfo(pool,types,value1,value2,value3):
        if(pool=="antpool"):
            global coin_type
            global sign_id
            global sign_key
            global sign_SECRET
            coin_type=types
            
            sign_id=value1
           
            sign_key=value2
           
            sign_SECRET=value3
            #print()
            return get_messages(html_hashrate_user)
        elif(pool=="f2pool"):
                #🔥type为货币类型 value1为f2pool用户名
                url= "https://api.f2pool.com/"+types+"/" + value1
                request = requests.get(url)
                #print(request.text)
                return request.text
        elif(pool=="poolin"):
                #🔥type为货币类型 value1为币印puid value2为币印coin_type value3为token
                url = "https://api-prod.poolin.com/api/public/v2/worker/stats"
                param={"puid":value1,"coin_type":types}
                header = {"Authorization":"Bearer "+value2}
                request = requests.get(url,headers=header,params=param,stream=True)
                #print(request.text)
                return (request.text)
        else:
             print("fuck off")


        return ("qwq")

##🔥antpool相关验签函数
def get_signature():
    nonce=int(time.time()*1000)
    msgs=sign_id+sign_key+str(nonce)
    ret=[]
    ret.append(hmac.new(sign_SECRET.encode(encoding="utf-8"), msg=msgs.encode(encoding="utf-8"), digestmod=hashlib.sha256).hexdigest().upper())#sign
    ret.append(nonce)#timestamp
    return ret
def get_messages(url):#POST
    # print("🔥"+coin_type)
    api_sign=get_signature()
    post_data = {'key': sign_key, 'nonce': api_sign[1],'signature': api_sign[0],'coin':coin_type}#post
    request = requests.post(url, data=post_data)
    return(request.text)

#🍺antpool debug
#getinfo("antpool","BTC","wikighome","f2204ac7dfa24c51b65f46014e34fe4a","e2a2797ce8d3493da7ec02ac1dc7072f")
#🍺f2pool debug
# data=getinfo("f2pool","bitcoin","wikig16","","")

#🍺poolin debug
#data=getinfo("poolin","btc","9264247","wowlDJUJqS0QiBkCKGBcBPWuU5kEIKopg9zDjt7lasSLDn8s8mEzQQxhBiRLLWbW","")
#print(data)
# if(len(sys.argv)==4):
#     getinfo(sys.argv[1],sys.argv[2],sys.argv[3],"","")
# if(len(sys.argv)==5):
#     getinfo(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4],"")
# if(len(sys.argv)==6):
#     getinfo(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4],sys.argv[5])
#🍺接口说明:
#调用格式:
#python ./index.py {矿池类型} {币种类型} {参数1} {参数2} {参数3}
#🍺参数对照:
#🚀f2pool:
#参数1:用户机器组组名
#🚀poolin:
#参数1:PUID
#参数2:观察者token
#🚀antpool
#参数1:群组名称
#参数2:apikey
#参数3:apisecret
